/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;

import Clases.Gasto;
import Clases.Paginacion;
import Persistencia.PersistenciaGasto;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author JAVA
 */
public class SFiltroFechasGasto extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        
        String fechaInicio = null;
        if(request.getParameter("finicio")!=null){
            fechaInicio = request.getParameter("finicio");
        }else if(request.getAttribute("finicio")!=null){
            fechaInicio = (String)request.getAttribute("finicio");
        }
        
        String fechaFin = null;
        if(request.getParameter("ffin")!=null){
            fechaFin = request.getParameter("ffin");
        }else if (request.getAttribute("finicio")!=null){
            fechaFin = (String)request.getAttribute("ffin");
        }
        Date fechaI = null;
        Date fechaF = null;
        Paginacion pagGastos = null;
        List<Gasto> gastos = null;
        int error = 0;
        
        try{
            fechaI = df.parse(fechaInicio);
            fechaF = df.parse(fechaFin);
        }catch(Exception ex){
            try {
                fechaI = df.parse(fechaInicio.replace("/", "-"));
                fechaF = df.parse(fechaFin.replace("/", "-"));
            } catch (ParseException ex1) {
                fechaI = null;
                fechaF = null;
            }
        }
        
        if(fechaF==null || fechaI==null){
            error = 1;
        }else{
            if(request.getParameter("pagina")==null){
                pagGastos = new Paginacion(1, 10, PersistenciaGasto.totalGastosFechas(fechaI, fechaF));
            }else {
                int pagina = Integer.parseInt(request.getParameter("pagina"));
                pagGastos = new Paginacion(pagina, 10, PersistenciaGasto.totalGastosFechas(fechaI, fechaF));
            }
                   
            gastos = PersistenciaGasto.obtenerGastosFechasPaginacion(fechaI, fechaF, pagGastos.por_pagina, pagGastos.offset());
            request.setAttribute("paginas", pagGastos.total_paginas());
            request.setAttribute("gastos", gastos);
        }
        
        request.setAttribute("error", error);
        
        // REDIRECCION
        RequestDispatcher rd = request.getRequestDispatcher("verGastos.jsp");
        rd.forward(request, response);
        
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet SFiltroFechasGasto</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet SFiltroFechasGasto at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
